#!/bin/sh

set -e

/etc/init.d/fcron stop

#disable things from grsec for building that we normally want enabled
sysctl -w kernel.grsecurity.chroot_deny_mknod=0 || echo "already disabled"
sysctl -w kernel.grsecurity.chroot_caps=0 || echo "already disabled"
ulimit -Hn 8192
ulimit -Sn 4096

#start a new screen session for the builds
screen -d -m -S catalyst -t catalyst bash

#attach to screen and auto-detach or it refuses to accept commands
#recurse screen inside screen or it won't run
screen -d -m screen -r catalyst
sleep 1
screen -d catalyst

#now run what we want

#take the snapshot
#screen -S catalyst -p 0 -X stuff 'ulimit -Hn 8192; ulimit -Sn 4096; find /catalyst/log -type f ! -name "summary.log*" -mtime +7 -delete ; ./snap.sh'$(printf \\r)
ulimit -Hn 8192; ulimit -Sn 4096; find /catalyst/log -type f ! -name "summary.log*" -mtime +7 -delete ; ./snap.sh

#kick off amd64 build

screen -S catalyst -p 0 -X stuff 'ulimit -Hn 8192; ulimit -Sn 4096; ./run_catalyst.sh amd64 hardened && ./run_catalyst.sh amd64 default'$(printf \\r)
#screen -S catalyst -p 0 -X stuff './run_catalyst.sh amd64 hardened'$(printf \\r)
#screen -S catalyst -p 0 -X stuff './run_catalyst.sh amd64 default'$(printf \\r)

#to keep io down we stagger the builds just a bit
screen -S catalyst -p 1 -X screen bash
screen -S catalyst -p 1 -X stuff 'ulimit -Hn 8192; ulimit -Sn 4096; sleep 15m'$(printf \\r)
#screen -S catalyst -p 1 -X stuff './make_modules.sh'$(printf \\r)

screen -S catalyst -p 1 -X stuff 'ulimit -Hn 8192; ulimit -Sn 4096; ./run_catalyst.sh i686 hardened && ./run_catalyst.sh i686 default'$(printf \\r)
#screen -S catalyst -p 1 -X stuff './run_catalyst.sh i686 hardened'$(printf \\r)
#screen -S catalyst -p 1 -X stuff './run_catalyst.sh i686 default'$(printf \\r)

screen -xr catalyst
